package com.hairline.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hairline.bean.Log;
import com.hairline.bean.LogExample;
import com.hairline.bean.vo.LogListVO;
import com.hairline.mapper.LogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    LogMapper logMapper;

    @Override
    public void setLog(Log log) {
        logMapper.insertSelective(log);
    }

    @Override
    @SuppressWarnings("ALL")
    public LogListVO logList(Integer page, Integer limit, String name, String sort, String order) {
        LogListVO logListVO = new LogListVO();

        PageHelper.startPage(page, limit);
        LogExample example = new LogExample();
        LogExample.Criteria criteria = example.createCriteria();
        if (name != null) {
            criteria.andAdminLike("%" + name + "%");
        }
        example.setOrderByClause(sort + " " + order);
        criteria.andDeletedEqualTo(false);

        List<Log> logs = logMapper.selectByExample(example);
        long total = new PageInfo<Log>(logs).getTotal();

        logListVO.setItems(logs);
        logListVO.setTotal(total);
        return logListVO;
    }
}
